<?php
namespace jy_wuliu;
defined('BY_JYA') or exit('error');
/*
	工具类
*/
class member extends \plugin{
	public function getinfo($mid='') {
		$mid=$mid?:$_SESSION['uid'];
		$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','groupid','avatar','nickname','wechat','realname','pcd'));
		$user=pdo_get('jy_wuliu_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','title','logo','type'));
		$member=array_merge($member,$user);
		return $member;
	}
	/*
		申请成为
	*/
	public function apply_merch($type,$mid=''){
		$mid=$mid?:$_SESSION['uid'];
		$apply=$this->get('apply',array('mid'=>$mid));
		if($apply && $apply['status']!=2){
			return;
		}
		$new_apply=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'type'=>$type,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('apply',$new_apply);
		$apply_id=$this->insertid();
		if(!$this->setting['ischezhu_shenhe'] && $type==1){
			//分销商无需审核，此处自动审核
			$this->check_apply($apply_id,1);
		}
		if(!$this->setting['ischezhu_shenhe'] && $type==2){
			//分销商无需审核，此处自动审核
			$this->check_apply($apply_id,1);
		}
		return true;
	}
	/*
		审核分销商
	*/
	public function check_apply($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('apply',array('id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			$member_update=array(
				'type'=>$apply['type'],
				'applytime'=>TIMESTAMP,
			);
			$this->update('member',$member_update,array('mid'=>$apply['mid']));
		}else{
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('id'=>$apply_id));
		return true;
	}
	/*
		禁用用户
	*/
	public function stop_merch($mid){
		$this->update('member',array('isblack'=>1),array('mid'=>$mid));
	}
	/*
		恢复用户
	*/
	public function start_merch($mid){
		$this->update('member',array('isblack'=>0),array('mid'=>$mid));
	}
}